/**
 * Created by zeopean on 2015/10/30.
 */
$(function(){
    /** 项目融资记录 */
    $('.save_log').bind('click' , function(){
        var Proid = $('#project_id').val();
        if(Proid == 0){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        $('#form_log input[name=project_id]').val(Proid);
        var logId   = $('input[name=log_id]').val();
        var url     = EditInvestLog + logId ;
        var org     = $('input[name=org_id]').val();
        var user    = $('input[name=user_id]').val();
        var account = $('input[name=invest_account]').val();
        if(!account)  {
            noty({text:'请填写投资金额',type:'warning',timeout:500});
            return;
        }

        if(org =='' && user==''){
            noty({text:'请选择投资人或者投资机构',type:'warning',timeout:500});
            return false;
        }

        $.ajax({
            type:"get",
            url:url,
            dataType:"json",
            data:$('#form_log').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});

                    if(org != 0){

                        $.ajax({
                            type: "get",
                            url: getInvestLogByProjectId,
                            dataType: "json",
                            data: {
                                'proId' : Proid
                            },
                            success: function (obj) {
                                var Str     = '<tr class="th"><th>编号</th><th>融资金额</th><th>融资时间</th><th>投资机构</th><th>投资人</th><th>操作</th></tr>' ;
                                var data    = obj.data;
                                var key     = 1;
                                for(var i in data){
                                    /** 对返回值做判断 */
                                    console.log(data);
                                    if(data[i].invest_account==null)
                                        data[i].invest_account = '暂无数据';
                                    if(data[i].invest_time==null)
                                        data[i].invest_time = '暂无数据';
                                    if(data[i].investor==null)
                                        data[i].investor = '暂无数据';
                                    if(data[i].org==null)
                                        data[i].org = '暂无数据';


                                    Str +='<tr><td>'+key+'</td>'
                                    +'<td class="account">'+data[i].invest_account+'</td>'
                                    +'<td>'+data[i].invest_time.substr(0,10)+'</td>'
                                    +'<td>'+data[i].org+'</td>'
                                    +'<td>'+data[i].investor+'</td>'
                                    +'<td><a class="edit-log" href="javascript:;" data="'+data[i].id+'">编辑</a>&nbsp;&nbsp;'
                                    +'<a class="del-log"  href="javascript:;" data="'+data[i].id+'">删除</a>'
                                    +'</td></tr>' ;
                                    key++;
                                }
                                $('.invest-log ').html(Str);
                            }
                        });
                    }
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });

    /** 删除功能 */
        $(document).on('click' , '.del-log' , function(){
        var tmp     = $(this);
        var logId   = tmp.attr('data');
        $.ajax({
            type: "get",
            url: delInvestLogById,
            dataType: "json",
            data: {
                'logId': logId
            },
            success: function (obj) {
                tmp.parents('tr').hide();
            }
        });
    });


    /** 编辑功能 **/
    $(document).on('click' , '.edit-log' , function(){
        var tmp     = $(this);
        var logId   = tmp.attr('data');
        var tr      = tmp.parents('tr');
        var account = tr.children('.account').html();
        var time    = tr.children('.time').html();

        $('input[name=invest_account]').val(account);
        $('#logDate .date').val(time);

        $('input[name=log_id]').val(logId);
    });

    /** end */
});